package lab3;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class XMLReadUtil {

   public static List<String> readXML(String fileName,String nodeName){
       List<String> visitorNameList = new ArrayList<>();
        try {
            File file = new File(fileName);
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(file);

            NodeList nodeList = doc.getElementsByTagName(nodeName);
//读取每个node
            for(int i = 0; i < nodeList.getLength(); ++i){
                Node node = nodeList.item(i);
                visitorNameList.add(node.getTextContent());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return visitorNameList;
    }
}
